package com.ykht.proj.comsrv.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Configuration
@ConditionalOnClass(WxMaProperties.class)
@EnableConfigurationProperties(WxMaProperties.class)
@AllArgsConstructor
public class WxMaConfiguration {

	@Resource
	private WxMaProperties properties;

	@Bean
	@ConditionalOnMissingBean
	public WxMaService wxMaService() {
		WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
		config.setAppid(properties.getAppid());
		config.setSecret(properties.getSecret());
		config.setToken(properties.getToken());
		config.setAesKey(properties.getAesKey());
		config.setMsgDataFormat(properties.getMsgDataFormat());

		WxMaService wxMaService = new WxMaServiceImpl();
		wxMaService.setWxMaConfig(config);
		return wxMaService;
	}

}